home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / sweep_next / sweep.c < prev    next >
C/C++ Source or Header  |  1993-07-06  |  4KB  |  175 lines

  1. #include <math.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "ooglutil.h"
  5. #include "hpoint3.h"
  6. #include "point3.h"
  7. #include "vectP.h"
  8. #include "transform.h"
  9. #include "transform3.h"
  10. #include "sweep.h"
  11.  
  12. static char msg[] = "sweep.c";
  13.  
  14. Geom *TranslationSweep(float length, Point3 *p, Geom *g) {
  15.   int h, i, j, k;
  16.   Vect *v;
  17.   Geom *new;
  18.   int nverts;
  19.   int npoly;
  20.   HPoint3 hp3, *point4;
  21.   int *nvert, *vert;
  22.  
  23.   if (g == NULL) return NULL;
  24.   if (strcmp(GeomName(g), "vect")) {
  25.     OOGLError(1, "TranslationSweep called with non-vector object.");
  26.     return NULL;
  27.   }
  28.  
  29.   v = (Vect *) g;
  30.   Pt3ToPt4(p, &hp3, 1);
  31.   /* Doesn't seem to be a HPt3Unit right now.  Drat! */
  32.   Pt3Unit(&hp3);
  33.   hp3.x *= length;
  34.   hp3.y *= length;
  35.   hp3.z *= length;
  36.   nverts = abs(v->nvert);
  37.  
  38.   point4 = OOGLNewNE(HPoint3, nverts * 2, msg);
  39.   for (i = 0; i < nverts; i++) {
  40.     HPt3Copy(&v->p[i], &point4[i]);
  41.     HPt3Copy(&v->p[i], &point4[i + nverts]);
  42.     HPt3Add(&point4[i + nverts], &hp3, &point4[i + nverts]);
  43.   }
  44.  
  45.   nvert = OOGLNewNE(int, nverts, msg);
  46.   vert = OOGLNewNE(int, nverts * 4, msg);
  47.   for (i = 0; i < nverts; i++) nvert[i] = 4;
  48.   for (h = i = k = 0; i < v->nvec; i++) {
  49.     for (j = 0; j < abs(v->vnvert[i]);) {
  50.       vert[h++] = k + j;
  51.       vert[h++] = k + nverts + j++;
  52.       vert[h++] = k + nverts + j;
  53.       vert[h++] = k + j;
  54.     }
  55.     if (j) h -= 4;
  56.     if (v->vnvert[i] < 0) {
  57.       vert[h++] = k;
  58.       vert[h++] = k + nverts;
  59.       vert[h++] = k + nverts + j - 1;
  60.       vert[h++] = k + j - 1;
  61.     } 
  62.     k += j;
  63.   }
  64.  
  65.   npoly = h / 4;
  66.  
  67.   new = GeomCreate("polylist",
  68.            CR_NPOLY, npoly,
  69.            CR_POINT4, point4,
  70.            CR_NVERT, nvert,
  71.            CR_VERT, vert,
  72.            CR_END);
  73.  
  74.   OOGLFree(point4);
  75.   OOGLFree(nvert);
  76.   OOGLFree(vert);
  77.  
  78.   return(new);
  79.  
  80. }
  81.  
  82. Geom *RotationSweep(float angle, Point3 *end, Point3 *axis, 
  83.             int divisions, Geom *g) {
  84.   int h, i, j, k, m;
  85.   Vect *v;
  86.   Geom *new;
  87.   int v_nvert;
  88.   int npoly;
  89.   HPoint3 *point4;
  90.   int *nvert, *vert;
  91.   Transform T, TInv, R;
  92.   int fullcircle = 0;
  93.  
  94.   if (g == NULL) return NULL;
  95.   if (strcmp(GeomName(g), "vect")) {
  96.     OOGLError(1, "RotationSweep called with non-vector object.");
  97.     return NULL;
  98.   }
  99.  
  100.   /* Hack to deal with things that should go all the way around */
  101.   if (fabs(angle - 2.0 * M_PI) < .01) fullcircle = 1;
  102.  
  103.   v = (Vect *) g;
  104.  
  105.   v_nvert = abs(v->nvert);
  106.  
  107.   TmTranslate(T, end->x, end->y, end->z);
  108.   TmInvert(T, TInv);
  109.   TmRotate(R, angle / (float)divisions, axis); 
  110.  
  111.   point4 = OOGLNewNE(HPoint3, v_nvert * (divisions + 1), msg);
  112.   for (i = 0; i < v_nvert; i++) {
  113.     HPt3Copy(&v->p[i], &point4[i * (divisions+1)]);
  114.     for (j = 1; j < divisions+1; j++) {
  115.       HPt3Copy(&point4[i*(divisions+1) + j - 1], 
  116.            &point4[i*(divisions+1) + j]);
  117.       HPt3Transform(TInv, &point4[i*(divisions+1) + j], 
  118.             &point4[i*(divisions+1) + j]);
  119.       HPt3Transform(R, &point4[i*(divisions+1) + j], 
  120.             &point4[i*(divisions+1) + j]);
  121.       HPt3Transform(T, &point4[i*(divisions+1) + j],
  122.             &point4[i*(divisions+1) + j]);
  123.     }
  124.   }
  125.  
  126.   nvert = OOGLNewNE(int, v_nvert * (divisions+1), msg);
  127.   vert = OOGLNewNE(int, v_nvert * (divisions+1) * 4, msg);
  128.   for (i = 0; i < v_nvert * (divisions+1); i++) nvert[i] = 4;
  129.   for (h = i = k = 0; i < v->nvec; i++) {
  130.     for (j = 0; j < abs(v->vnvert[i]); j++) {
  131.       for (m = 1; m < divisions + 1; m++) {
  132.     vert[h++] = ((k + j)*(divisions+1)) + m - 1;
  133.     vert[h++] = ((k + j)*(divisions+1)) + m;
  134.     vert[h++] = ((k + j + 1)*(divisions+1)) + m;
  135.     vert[h++] = ((k + j + 1)*(divisions+1)) + m - 1;
  136.       }
  137.       if (fullcircle) {
  138.     vert[h-2] = (k + j + 1) * (divisions + 1);
  139.     vert[h-3] = (k + j) * (divisions + 1);
  140.       }
  141.     }
  142.     if (j) h -= 4 * divisions;
  143.     if (v->vnvert[i] < 0) { 
  144.       for (m = 1; m < divisions + 1; m++) {
  145.     vert[h++] = (k) * (divisions + 1) + m - 1;
  146.     vert[h++] = (k) * (divisions + 1) + m;
  147.     vert[h++] = (k + j - 1) * (divisions + 1) + m;
  148.     vert[h++] = (k + j - 1) * (divisions + 1) + m - 1;
  149.       }
  150.       if (fullcircle) {
  151.     vert[h-2] = (k + j - 1) * (divisions + 1);
  152.     vert[h-3] = k * (divisions + 1); 
  153.       }
  154.     }
  155.     k += j;
  156.   }
  157.   
  158.   npoly = h / 4;
  159.  
  160.   new = GeomCreate("polylist",
  161.            CR_NPOLY, npoly,
  162.            CR_POINT4, point4,
  163.            CR_NVERT, nvert,
  164.            CR_VERT, vert,
  165.            CR_END);
  166.  
  167.   OOGLFree(point4);
  168.   OOGLFree(nvert);
  169.   OOGLFree(vert);
  170.  
  171.   return(new);
  172.  
  173. }
  174.  
  175.